package com.whfc.common.word;

import com.whfc.common.util.HttpUtil;
import com.whfc.common.util.ImageUtil;
import freemarker.template.TemplateMethodModelEx;
import freemarker.template.TemplateModelException;
import org.springframework.util.Base64Utils;

import java.io.InputStream;
import java.util.List;

/**
 * @author: hw
 * @date: 2021-11-13 17:24
 * @description: xml转doc url转换base64
 */
public class XmlTemplate2Base64EncoderMethodEx implements TemplateMethodModelEx {

    @Override
    public Object exec(List arguments) throws TemplateModelException {
        if (arguments == null || arguments.isEmpty() || arguments.get(0) == null) {
            return "";
        }
        String url = arguments.get(0).toString();
        InputStream is = HttpUtil.downLoad(url);
        byte[] bytes = ImageUtil.readInputStream(is);
        return Base64Utils.encodeToString(bytes);
    }
}
